"""Compute and plot distribution of tweets/user per video."""
import sys
import os
import math
import pylab as plt
from params import *
from utilities import *

popularity = {}
d = {}
d2 = {}
tweets = []
users = []
tracefile = os.path.join(WORKDIR, 'geocascades', 'traces',
    'tweets_per_video.txt')
for line in open(tracefile):
    hash,msgs = eval(line)
    l = len(msgs)
    l2 = len(set(u for t,u in msgs))
    tweets.append(l)
    users.append(l2)
    popularity[hash] = l2
    if not l in d:
        d[l] = 0
    d[l] += 1
    if not l2 in d2:
        d2[l2] = 0
    d2[l2] += 1

x = sorted([l for l in d])
y = [d[l] for l in d]

plt.figure()
plt.clf()
plt.axes(FIG_AXES2)
plt.loglog(x,y,'ko', mfc='None')
plt.xlabel('Number of tweets')
plt.ylabel('Number of videos')
[x1,x2,y1,y2] = plt.axis()
plt.axis([x1,x2,1,y2])
plt.grid(True)
plt.savefig('popularity_tweets.pdf')
plt.close()


x2 = sorted([l for l in d2])
y2 = [d2[l] for l in d2]

plt.figure()
plt.clf()
plt.axes(FIG_AXES2)
plt.loglog(x2,y2,'ko', mfc='None')
plt.xlabel('Number of users')
plt.ylabel('Number of videos')
[x1,x2,y1,y2] = plt.axis()
plt.axis([x1,x2,1,y2])
plt.grid(True)
plt.savefig('popularity_users.pdf')
plt.close()

#x = sorted([l for l in d])
#y = [d[l] for l in d]
#x2 = sorted([l for l in d2])
#y2 = [d2[l] for l in d2]
#
#c = [y[0]]
#for i in range(1,len(y)):
#    t = c[i-1]
#    c.append(t+y[i])
#tot = c[-1]
#c = map(lambda i: 1-1.0*i/tot,c)
#
#c2 = [y2[0]]
#for i in range(1,len(y2)):
#    t = c2[i-1]
#    c2.append(t+y2[i])
#tot = c2[-1]
#c2 = map(lambda i: 1 - 1.0*i/tot,c2)
#
#plt.figure()
#plt.clf()
#plt.axes(FIG_AXES2)
#plt.loglog(x,c,'ko', mfc='None')
#plt.xlabel('Number of tweets')# Num
#plt.ylabel('Number of videos')
#[x1,x2,y1,y2] = plt.axis()
##plt.axis([x1,x2,1,y2])
#plt.grid(True)
#plt.savefig('cum_popularity2.pdf')
#plt.close()
